package jdbmaplite.util;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DBUtils {

	private static final Log _cat = LogFactory.getLog(DBUtils.class);

	/**
	 * Close Quietly
	 * 
	 * @param closeable
	 *            Closeable
	 */
	public static void closeQuietly(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
				_cat.warn("Close connection failed!", e);
			}
		}
	}

	/**
	 * Commit Quietly
	 * 
	 * @param conn
	 *            Connection
	 */
	public static void commitQuietly(Connection conn) {
		if (conn != null) {
			try {
				conn.commit();
			} catch (SQLException e) {
				_cat.warn("Commit connection failed!", e);
			}
		}
	}

	/**
	 * Close Quietly
	 * 
	 * @param conn
	 *            Connection
	 */
	public static void closeQuietly(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				_cat.warn("Close connection failed!", e);
			}
		}
	}

	/**
	 * Rollback Quietly
	 * 
	 * @param conn
	 *            Connection
	 */
	public static void rollbackQuietly(Connection conn) {
		if (conn != null) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				_cat.warn("Rollback connection failed!", e);
			}
		}
	}

	/**
	 * Rollback Quietly
	 * 
	 * @param conn
	 *            Connection
	 * @param savepoint
	 *            Savepoint
	 */
	public static void rollbackQuietly(Connection conn, Savepoint savepoint) {
		if (conn != null) {
			try {
				conn.rollback(savepoint);
			} catch (SQLException e) {
				_cat.warn("Rollback connection failed!", e);
			}
		}
	}

}